// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.chat.defaultChats;

import java.nio.ByteBuffer;
import zombie.Lua.LuaManager;
import zombie.characters.IsoPlayer;
import zombie.chat.ChatBase;
import zombie.chat.ChatMessage;
import zombie.chat.ChatSettings;
import zombie.chat.ChatTab;
import zombie.core.Color;
import zombie.core.Core;
import zombie.core.network.ByteBufferWriter;
import zombie.debug.DebugLog;
import zombie.network.chat.ChatType;

public class WhisperChat extends ChatBase {
    private String myName;
    private String companionName;
    private final String player1;
    private final String player2;
    private boolean isInited = false;

    public WhisperChat(int id, ChatTab tab, String firstMember, String secondMember) {
        super(id, ChatType.whisper, tab);
        if (!this.isCustomSettings()) {
            this.setSettings(getDefaultSettings());
        }

        this.player1 = firstMember;
        this.player2 = secondMember;
    }

    public WhisperChat(ByteBuffer bb, ChatTab tab, IsoPlayer owner) {
        super(bb, ChatType.whisper, tab, owner);
        if (!this.isCustomSettings()) {
            this.setSettings(getDefaultSettings());
        }

        this.player1 = zombie.GameWindow.ReadString(bb);
        this.player2 = zombie.GameWindow.ReadString(bb);
    }

    public static ChatSettings getDefaultSettings() {
        ChatSettings chatSettings = new ChatSettings();
        chatSettings.setBold(true);
        chatSettings.setFontColor(new Color(85, 26, 139));
        chatSettings.setShowAuthor(true);
        chatSettings.setShowChatTitle(true);
        chatSettings.setShowTimestamp(true);
        chatSettings.setUnique(true);
        chatSettings.setAllowColors(true);
        chatSettings.setAllowFonts(true);
        chatSettings.setAllowBBcode(true);
        return chatSettings;
    }

    @Override
    public String getMessagePrefix(ChatMessage msg) {
        if (!this.isInited) {
            this.init();
        }

        StringBuilder stringBuilder = new StringBuilder(this.getChatSettingsTags());
        if (this.isShowTimestamp()) {
            stringBuilder.append("[").append(LuaManager.getHourMinuteJava()).append("]");
        }

        if (this.isShowTitle()) {
            stringBuilder.append("[").append(this.getTitle()).append("]");
        }

        if (!this.myName.equalsIgnoreCase(msg.getAuthor())) {
            stringBuilder.append("[").append(this.companionName).append("]");
        } else {
            stringBuilder.append("[to ").append(this.companionName).append("]");
        }

        stringBuilder.append(": ");
        return stringBuilder.toString();
    }

    @Override
    protected void packChat(ByteBufferWriter byteBufferWriter) {
        super.packChat(byteBufferWriter);
        byteBufferWriter.putUTF(this.player1);
        byteBufferWriter.putUTF(this.player2);
    }

    public String getCompanionName() {
        return this.companionName;
    }

    public void init() {
        if (this.player1.equals(IsoPlayer.getInstance().getUsername())) {
            this.myName = IsoPlayer.getInstance().getUsername();
            this.companionName = this.player2;
        } else {
            if (!this.player2.equals(IsoPlayer.getInstance().getUsername())) {
                if (Core.bDebug) {
                    throw new RuntimeException("Wrong id");
                }

                DebugLog.log("Wrong id in whisper chat. Whisper chat not inited for players: " + this.player1 + " " + this.player2);
                return;
            }

            this.myName = IsoPlayer.getInstance().getUsername();
            this.companionName = this.player1;
        }

        this.isInited = true;
    }

    public static enum ChatStatus {
        None,
        Creating,
        PlayerNotFound;
    }
}
